package org.honeynet.hbstats.statservlet.common;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class JsonGenerator {

    ByteArrayOutputStream o = new ByteArrayOutputStream();
    org.codehaus.jackson.JsonGenerator jg;

    public JsonGenerator() throws IOException {
        JsonFactory jsonFactory = new JsonFactory();
        jg = jsonFactory.createJsonGenerator(o, JsonEncoding.UTF8);
    }

    public byte[] getByteArray() {
        return o.toByteArray();
    }

    public void writeStartArray() throws IOException, JsonGenerationException {
        jg.writeStartArray();
    }

    public void writeEndArray() throws IOException, JsonGenerationException {
        jg.writeEndArray();
    }

    public void writeNumber(long v) throws IOException, JsonGenerationException {
        jg.writeNumber(v);
    }

    public void writeString(String text) throws IOException, JsonGenerationException {
        jg.writeString(text);
    }

    public void close() throws IOException {
        jg.close();
        o.close();
    }
}
